//
//  MainViewController.swift
//  swiftwb
//
//  Created by 凌空 on 16/1/23.
//  Copyright © 2016年 fharmony. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

//    已经不推荐在didload中设置frame大小，因为这时候其他的组件可能还没有frame
    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildVC()
        
        view.backgroundColor = UIColor.redColor()
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        setupComposeBtnLayout()
    }

    private func setupComposeBtnLayout(){
        
        let width = Int(CGRectGetWidth(UIScreen.mainScreen().bounds))/(viewControllers?.count)!
        composeBtn.frame = CGRect(x: 2*width, y: 4, width: width, height: 45)
        tabBar.addSubview(composeBtn)
    }
    
    private func addChildVC(){
        let path = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil)
        if let jsonPath = path{
            let jsonData =  NSData(contentsOfFile: jsonPath)
            //            文件的加载使用do...catch
            do{
                //            将有可能发生异常的代码放在这里
                let dictArr = try NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.MutableContainers)
                //                需要提供枚举出来的字典类型
                for dic in dictArr as! [[String:String]]{
                    //                  原来的方法里面需要的是一定有值的变量，所以需要加！
                    addChildViewController(dic["vcName"]!, title: dic["title"]!, imageName: dic["imageName"]!)
                }
            }catch{
                //                这里添加普通的代码
                addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
                addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                addChildViewController("NullViewController", title: "", imageName: "")
                addChildViewController("DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
                addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
                
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    private func addChildViewController(childController: String,title:String,imageName:String) {
        
        //通过名称产生类
        let ns = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as! String
        let cls = NSClassFromString(ns+"."+childController)
        let vcCls = cls as! UIViewController.Type
        let vc = vcCls.init()
        vc.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.selectedImage =  UIImage(named: imageName+"_highlighted")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.title = title
        let nav = UINavigationController()
        nav.addChildViewController(vc)
        addChildViewController(nav)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    // MARK: - lazy
//    返回值是UIButton，所以在变量后面需要添加:UIButton
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "composeBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
//    监听方法不能使用private
    func composeBtnClick(){
        print("\("Yoo")")
    }

}
